package com.taoyibao.hongcanting.ui.mine.fragment;


import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.fragment.BaseFragmentPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.dialog.DialogComment;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.ui.home.delegate.MineFragmentDelegate;
import com.taoyibao.hongcanting.ui.login.activity.LoginActivity;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;
import com.taoyibao.hongcanting.utils.permission.MPermission;
import com.taoyibao.hongcanting.utils.permission.OnMPermissionDenied;
import com.taoyibao.hongcanting.utils.permission.OnMPermissionGranted;
import com.taoyibao.hongcanting.utils.permission.OnMPermissionNeverAskAgain;

import java.util.List;

/**
 * @author TangAnna
 * @description: 餐厅模块
 * @date :${DATA} 14:52
 */
public class MineFragment extends BaseFragmentPresenter<MineFragmentDelegate> implements View.OnClickListener {
    private ImageView mCircleImageView;
    private static final int REQUEST_CODE_PERMISSION = 102;
    //需要申请的权限的数组
    private String[] mPermissions = {Manifest.permission.CALL_PHONE};

    @Override
    protected Class<MineFragmentDelegate> getDelegateClass() {
        return MineFragmentDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        mCircleImageView = viewDelegate.get(R.id.iv_mine_head);
        viewDelegate.get(R.id.layout_mine_call).setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && viewDelegate != null) {
            resume();
        }
    }

    /**
     * 每次显示都要调用的方法
     */
    public void resume() {
        if (SpUtils.getLoginStatus()) {
            getUserInfo();

        } else {
            viewDelegate.setUserData(null);
        }
    }

    /**
     * 获取用户资料
     */
    public void getUserInfo() {
        if (SpUtils.getLoginStatus()) {
            JsonApiWrapper.getApiService().userInfo(SpUtils.getToken(), SpUtils.getMemberId())
                    .compose(new DefaultTransformer<CompanyBaseModel<List<UserInfoModel>>>())
                    .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserInfoModel>>>(getContext(), false) {
                        @Override
                        public void onNext(CompanyBaseModel<List<UserInfoModel>> result) {
                            if (result.code == NetCode.HTTP_200 && result.data != null && result.data.size() > 0) {
                                UserInfoModel userInfoModel = result.data.get(0);
                                viewDelegate.setUserData(userInfoModel);
                            } else {
                                super.onNext(result);
                            }
                        }
                    });

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    //同意
    @RequiresApi(api = Build.VERSION_CODES.M)
    @OnMPermissionGranted(REQUEST_CODE_PERMISSION)
    public void onBasicPermissionSuccess() {
        call(CodeUtils.getIdString(R.string.mine_phone));
    }

    //拒绝
    @OnMPermissionDenied(REQUEST_CODE_PERMISSION)
    public void onBasicPermissionFailed() {
        ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintPermission));
    }

    //不再提醒
    @OnMPermissionNeverAskAgain(REQUEST_CODE_PERMISSION)
    public void onBasicPermissionNeverAskAgain() {
        ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintPermission));
    }

    /**
     * 拨打电话
     */
    public void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    /**
     * 基本权限管理
     */
    public void requestBasicPermission(String[] permission, int requestCode) {
        MPermission.with(this)
                .addRequestCode(requestCode)
                .permissions(
                        permission
                ).request();
    }

    @Override
    public void onClick(View v) {
        if (SpUtils.getLoginStatus()) {
            switch (v.getId()) {
                case R.id.layout_mine_call:
                    DialogComment dialogComment = new DialogComment(CodeUtils.getIdString(R.string.mine_call), new DialogComment.DialogCommentInterfaca() {
                        @Override
                        public void onConfirmListener() {
                            requestBasicPermission(mPermissions, REQUEST_CODE_PERMISSION);
                        }
                    });
                    dialogComment.showDilaog(getChildFragmentManager(), "MineFragmentDelegate");
                    break;
            }
        } else {
            startActivity(new Intent(getContext(), LoginActivity.class));
        }
    }
}
